package com.hd.trans.files.model;

import android.app.Application;
import android.util.Pair;

import androidx.annotation.NonNull;
import androidx.lifecycle.LifecycleOwner;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
import androidx.lifecycle.ViewModelProvider;

import com.hd.trans.R;
import com.hd.trans.files.bean.Audio;
import com.hd.trans.files.bean.Video;
import com.hd.trans.files.fragment.MediaListFragment;
import com.hd.trans.utils.FileUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class PickingFileViewModel extends BaseViewModel {

    public static final String TAG = "AudioExportViewModel";

    public static final int TAB_MEDIA_LIB = 0;
    public static final int TAB_MEDIA_QQ = 1;
    public static final int TAB_MEDIA_WX = 2;

    private final int translationType;

    private final File mStorageRoot = new File(FileUtils.getStorageDirectory());
    private String mCurrentPath = mStorageRoot.getAbsolutePath();
    private final List<Audio> mSelectedAudios = new ArrayList<>();
    // 选中的视频原数据
    private final List<Video> mSelectedVideos = new ArrayList<>();

    private final MutableLiveData<List<TabViewUiModel>> mTabLayoutObserver = new MutableLiveData<>();
    private final MutableLiveData<List<Audio>> mSelectedAudiosObserver = new MutableLiveData<>();
    private final MutableLiveData<Integer> mMaxCountObserver = new MutableLiveData<>();
    private final MutableLiveData<NextButtonUiModel> mEnableNextObserver = new MutableLiveData<>();
    private final MutableLiveData<String> mCurrentPathObserver = new MutableLiveData<>(mCurrentPath);
    private final MutableLiveData<String> mBackPressedObserver = new MutableLiveData<>();
    private final MutableLiveData<Boolean> mFinishActivityObserver = new MutableLiveData<>();
    private final MutableLiveData<String> mSearchObserver = new MutableLiveData<>();

    // 选中的视频数据：first 所有选中的视频，second 当前选中的视频
    private final MutableLiveData<Pair<List<Video>, Video>> mSelectedVideoObserver = new MutableLiveData<>();
    private final MutableLiveData<List<String>> mAudioExtractionFailedObserver = new MediatorLiveData<>();

    // 格式转换
    private final MutableLiveData<List<String>> mAudioConvertResultObserver = new MediatorLiveData<>();

    public PickingFileViewModel(@NonNull Application application, List<Audio> selectedAudios, int translationType) {
        super(application);
        if (selectedAudios != null) {
            mSelectedAudios.clear();
            mSelectedAudios.addAll(selectedAudios);
        }
        this.translationType = translationType;
        setProgressUiViewModel(new ProgressUiViewModel());
    }


    @Override
    public void onCreate(@NonNull LifecycleOwner owner) {
        setupTabLayout();
        updateAudioNextButton();
    }

    @Override
    public void onDestroy(@NonNull LifecycleOwner owner) {
    }


    public List<Audio> getSelectedAudios() {
        return mSelectedAudios;
    }


    public MutableLiveData<List<String>> getAudioConvertFailedObserver() {
        return mAudioConvertResultObserver;
    }

    public MutableLiveData<List<String>> getAudioExtractionFailedObserver() {
        return mAudioExtractionFailedObserver;
    }

    public MutableLiveData<Pair<List<Video>, Video>> getSelectedVideoObserver() {
        return mSelectedVideoObserver;
    }

    public MutableLiveData<String> getSearchObserver() {
        return mSearchObserver;
    }

    public MutableLiveData<String> getCurrentPathObserver() {
        return mCurrentPathObserver;
    }

    public MutableLiveData<List<TabViewUiModel>> getTabLayoutObserver() {
        return mTabLayoutObserver;
    }

    public MutableLiveData<NextButtonUiModel> getEnableNextObserver() {
        return mEnableNextObserver;
    }

    public MutableLiveData<List<Audio>> getSelectedAudiosObserver() {
        return mSelectedAudiosObserver;
    }

    public MutableLiveData<Integer> getMaxCountObserver() {
        return mMaxCountObserver;
    }

    public MutableLiveData<String> getBackPressedObserver() {
        return mBackPressedObserver;
    }

    public MutableLiveData<Boolean> getFinishActivityObserver() {
        return mFinishActivityObserver;
    }

    /**
     * 初始化选择文件的 Tab 数据和 Fragment
     */
    public void setupTabLayout() {
        List<TabViewUiModel> tabs = new ArrayList<>();
        String[] tabsTitle = getApplication().getResources().getStringArray(R.array.picking_file_tabs);
        tabs.add(new TabViewUiModel(tabsTitle[0], R.drawable.ic_files_home_dir_icon, MediaListFragment.newInstance(TAB_MEDIA_LIB, translationType)));
        tabs.add(new TabViewUiModel(tabsTitle[1], R.drawable.ic_files_home_qq_icon, MediaListFragment.newInstance(TAB_MEDIA_QQ, translationType)));
        tabs.add(new TabViewUiModel(tabsTitle[2], R.drawable.ic_files_home_wechat_icon, MediaListFragment.newInstance(TAB_MEDIA_WX, translationType)));
        mTabLayoutObserver.setValue(tabs);
    }

    public void doSearchAudioWithKeyWord(String keyWord) {
        mSearchObserver.setValue(keyWord);
    }

    public void showCurrentPath() {
        mCurrentPathObserver.setValue(mCurrentPath);
    }

    /**
     * 更新文件路径
     *
     * @param path 当前路径
     */
    public void updateCurrentPath(String path) {
        this.mCurrentPath = path;
        showCurrentPath();
    }

    /**
     * 点击返回按钮
     *
     * @param path 当前的路径
     */
    public void onBackPressed(String path) {
        path = path.substring(5);
        if (path.equals(mStorageRoot.getAbsolutePath())) {
            // 已经在根目录，finish Activity
            mFinishActivityObserver.setValue(true);
        } else {
            // 没有在根目录
            mBackPressedObserver.setValue(path.substring(0, path.lastIndexOf("/")));
        }
    }

    /**
     * 更新选择文件
     */
    public void updateSelected(Audio audio) {
        /*if (!mSelectedAudios.contains(audio)) {
            if (mMaxSelectCount == 1 && mSelectedAudios.size() >= mMaxSelectCount) {
                mSelectedAudios.clear();
                mSelectedAudios.add(audio);
                mSelectedAudiosObserver.setValue(mSelectedAudios);
            } else if (mMaxSelectCount <= mSelectedAudios.size()) {
                mMaxCountObserver.setValue(mMaxSelectCount);
            } else {
                mSelectedAudios.add(audio);
                mSelectedAudiosObserver.setValue(mSelectedAudios);
            }
        } else {
            int index = mSelectedAudios.indexOf(audio);
            if (index >= 0 && index <= mSelectedAudios.size() && mSelectedAudios.get(index) != null) {
                mSelectedAudios.remove(index);
                mSelectedAudiosObserver.setValue(mSelectedAudios);
            }
        }*/
        updateAudioNextButton();
    }

    /**
     * 更新选中的视频
     */
    public void updateSelectedVideo(Video video) {
        if (mSelectedVideos.contains(video)) {
            mSelectedVideos.remove(video);
            mSelectedVideoObserver.setValue(new Pair<>(mSelectedVideos, video));
        } else {
           /* if (mSelectedVideos.size() >= mMaxSelectCount) {
                mMaxCountObserver.setValue(mMaxSelectCount);
            } else {
                mSelectedVideos.add(video);
                mSelectedVideoObserver.setValue(new Pair<>(mSelectedVideos, video));
            }*/
        }
        updateVideoNextButton();
    }

    /**
     * 更新下一步按钮状态
     */
    private void updateVideoNextButton() {
//        boolean enable = mSelectedVideos.size() >= mMinSelectCount && mSelectedVideos.size() <= mMaxSelectCount;
//        NextButtonUiModel buttonViewModel = new NextButtonUiModel(mSelectedVideos.size(), enable);
//        mEnableNextObserver.setValue(buttonViewModel);
    }

    /**
     * 更新下一步按钮状态
     */
    private void updateAudioNextButton() {
//        boolean enable = mSelectedAudios.size() >= mMinSelectCount && mSelectedAudios.size() <= mMaxSelectCount;
//        NextButtonUiModel buttonViewModel = new NextButtonUiModel(mSelectedAudios.size(), enable);
//        mEnableNextObserver.setValue(buttonViewModel);
    }


    public List<Video> getSelectedVideos() {
        return mSelectedVideos;
    }

    public static class PickingViewModelFactory implements ViewModelProvider.Factory {

        private final Application mApplication;
        private final List<Audio> mSelectedAudios;
        private final int translationType;

        public PickingViewModelFactory(Application application, ArrayList<Audio> selectedAudios, int translationType) {
            this.mApplication = application;
            this.mSelectedAudios = selectedAudios;
            this.translationType = translationType;
        }

        @NonNull
        @Override
        public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
            return (T) new PickingFileViewModel(mApplication, mSelectedAudios, translationType);
        }
    }
}
